﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Yuebon.Commons.Encrypt;
using Yuebon.Commons.IoC;
using Yuebon.Security.Dtos;
using Yuebon.Security.IServices;
using Yuebon.Security.Models;

namespace Yuebon.Security.Application
{
    public class UserApp
    {
        IUserService service = IoCContainer.Resolve<IUserService>();
        IUserLogOnService userLogOnService = IoCContainer.Resolve<IUserLogOnService>();

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <param name="token">token令牌</param>
        /// <returns></returns>
        public IEnumerable<User> GetAll()
        {
             return  service.GetAll();
        }

        /// <summary>
        /// 根据用户账号查询用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
       public User GetByUserName(string userName)
        {
            return service.GetByUserName(userName);
        }

        public bool ValidateCredentials(string userName,string password)
        {
            //获取用户信息
            User userInfo = new User();
            userInfo = service.GetByUserName(userName);

            if (userInfo == null)
            {
                throw new Exception("用户不存在");
            }
            var userSinginEntity = userLogOnService.GetByUserId(userInfo.Id);
            string inputPassword = MD5Util.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(password).ToLower(), userSinginEntity.UserSecretkey).ToLower()).ToLower();

            if (userSinginEntity.UserPassword != inputPassword)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
